import Vue from 'vue'
import VueRouter from 'vue-router'
import route from './index.js'

Vue.use(VueRouter)

//$router:VueRouter
//$route:当前路由对象

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    route
  })
  
  //定义路由栈
  //用于判断当前页面是否有上级页面
  
  var routeStack = ["/"]
  
  //路由守卫
  
  // router.beforeEach((to, from, next) => {
  //   if (to.meta.isAuth) {
  //     if (isLogin()) {
  //       next()
  //     } else {
  //       next()
  //     }
  //   } else {
  //     next()
  //   }
  // })
  
  //跳转方法
  //replaeRoute
  //切换顶层路由对象
  
  function repaceRoute(Path) {
    routeStack.pop()
    routeStack.push(path)
    router.replace(path)
  }
  
  //跳转下级页面
  // @param{*}path
  // @param{*}params
  
  function pushRouteByQuery(path, params) {
    routeStack.push(path)
    router.push({
      path: path,
      query: params
    })
  }
  
  function pushRouteByName(name, params) {
    routeStack.push(name)
    router.push({
      name: name,
      params: params
    })
  }
  
  function backRoute() {
    routeStack.pop()
    router.back()
  }
  
  //router添加对应方法
  router.repaceRoute = repaceRoute;
  router.pushRouteByQuery = pushRouteByQuery
  router.pushRouteByName = pushRouteByName
  router.backRoute = backRoute
  router.routeStack = routeStack
  
  export default router